package com.huang.servlet;

import com.huang.datebase.UserDel;
import com.huang.datebase.UserLog;
import com.huang.info.UserInfo;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;

@WebServlet("/del")
public class DelServlet extends HttpServlet {
	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

		HttpSession session = req.getSession();

		UserInfo userInfo = (UserInfo) session.getAttribute("userInfo");
		//是否存在用户信息，登录是否失效
		if (userInfo == null) {
			resp.sendRedirect("pages/login.jsp");
			return;
		}

		//删除用户逻辑
		String id = req.getParameter("id");
		if (id == null || "".equals(id)) {
			req.setAttribute("errMsg", "id为空");
			req.getRequestDispatcher("pages/error.jsp").forward(req, resp);
			return;
		}

		UserDel userDel = new UserDel();
		try {
			//删除成功，重新加载数据
			userDel.DelById(id);
			UserLog userLog = new UserLog();
			session.setAttribute("userList", userLog.queryAllUserInfo());
			req.getRequestDispatcher("pages/newMain.jsp").forward(req, resp);

		} catch (SQLException throwables) {
			req.setAttribute("errMsg", "删除失败");
			req.getRequestDispatcher("pages/error.jsp").forward(req, resp);
		}

	}
}
